//package com.sky.controller.user;
//
//
//import com.alipay.api.AlipayApiException;
//import com.alipay.api.AlipayClient;
//import com.alipay.api.DefaultAlipayClient;
//import com.alipay.api.internal.util.AlipaySignature;
//import com.alipay.api.request.AlipayTradePagePayRequest;
//import io.swagger.models.Model;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//import java.io.UnsupportedEncodingException;
//import java.text.SimpleDateFormat;
//import java.util.*;
//
//@Controller
//public class AliPayController {
////    @Autowired
////    DonaItemService donaItemService;
////    @Autowired
////    DonaService donaService;
//    //appid
//    private final String APP_ID = "2021000119625133";
//    //应用私钥
//    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCDA1Y0+IjB/z4sIhJA0sgEaNGXV9+8t0fUMhrbjHjW88tz8d7jR8ev1aFMmyDKy/cSnlsoNDLwx71GCLiNQENYTvqdcsrYLMvQkmeIYT1OWcPYSl65P0TQRDBhakVdAIXvnts7KJ/nCROGMy8qrBG8ilSlNaOkiuXgp1OPIqLe2dCkrJp1spOJ3Wfq9uyqJO5xrgx6tby3MVPLu/nJ2PtzXt62Kpcj/S9Xxxk9FXOw8cwGDStMPU471VKpEP4kMnjfpUpB9/FfQ6oDfokIaq9dehN9xSitAD6p5cFct7oG94DHai5a+ST6X+OfTv03oE2DuA7ejaJn6cDGSB4IYmZRuDGdicqmPmLEHt3Kq79+EPxBYKUcWI4zQPXlA06Kcgb8HrJntjC836svSUrewWaG32g5MlJB8e1Z+1yL75E6k6R4rBJiVW2MC5VW+p/nQiY3FLfRq4a8V+VJ9uXM1h7L8TbOHtCAQKBgQDCSljMB8VnLFMKHOrcHCZNtjnDHsRYJQYEgNhhvwx2xUIgZNhzpiDmUSwR4TOaqF+Eg5bLfC+sQboANXpP5YUyt/rqUWqQ1fmEV5US9cfiYOi5rfjETK0RAJZaqAVZ0b0PAOMoQpNSbiQKBgHtAnpTuE0QFby+7hDsnTz+qC9dyQQWH3cBOn4RQzo1DUxvyQpZjAy0Oqn/F5x6RGMQU6SrirdUQbGWcANOpp9/L3YGHUrUjlT5Ehx2nPO//yTZSTWKM+p6+XALn1DGZbTChnL/5aEZsg5R4f55wL6RYezRzhq+w4wMixTQDyFLZ";
//    private final String CHARSET = "UTF-8";
//    // 支付宝公钥
//    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3CPgueDLkfB66s9ZsEUwyUbmkRknTFVyuBG4PkKI93OTOVC457ijEKknRYi8eKYo4Wl+75KxYO+tTr1u3XQZmjtjlqbty50DmxRCgEqJKYEu6CD+r1vi+2SXOUKnCJzsE8vHojS+Vk5oGbZYnX6Esw2TVeiCkmQ814CBwIDAQAB";
//    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
//    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
//    private final String FORMAT = "JSON";
//    //签名方式
//    private final String SIGN_TYPE = "RSA2";
//    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
//    private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl";
//    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
//    private final String RETURN_URL = "http://localhost:8080/returnUrl";
//
//
//    //必须加ResponseBody注解，否则spring会寻找thymeleaf页面
//    @ResponseBody
//    @RequestMapping("/pay/alipay")
//    public String alipay(HttpSession session, Model model,
//                         @RequestParam("dona_money") float dona_money,
//                         @RequestParam("dona_id") int dona_id) throws AlipayApiException {
//        //把dona_id项目id 放在session中
//        session.setAttribute("dona_id",dona_id);
//
//        //生成订单号（支付宝的要求？）
//        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//        String user = UUID.randomUUID().toString().replace("-","").toUpperCase();
//
//        String OrderNum = time+user;
//
//        //调用封装好的方法（给支付宝接口发送请求）
//        return sendRequestToAlipay(OrderNum,dona_money,"ghjk");
//    }
//    /*
//参数1：订单号
//参数2：订单金额
//参数3：订单名称
// */
//    //支付宝官方提供的接口
//    private String sendRequestToAlipay(String outTradeNo,Float totalAmount,String subject) throws AlipayApiException {
//        //获得初始化的AlipayClient
//        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL,APP_ID,APP_PRIVATE_KEY,FORMAT,CHARSET,ALIPAY_PUBLIC_KEY,SIGN_TYPE);
//
//        //设置请求参数
//        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
//        alipayRequest.setReturnUrl(RETURN_URL);
//        alipayRequest.setNotifyUrl(NOTIFY_URL);
//
//        //商品描述（可空）
//        String body="";
//        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
//                + "\"total_amount\":\"" + totalAmount + "\","
//                + "\"subject\":\"" + subject + "\","
//                + "\"body\":\"" + body + "\","
//                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//
//        //请求
//        String result = alipayClient.pageExecute(alipayRequest).getBody();
//        return result;
//    }
//
//    @RequestMapping("/returnUrl")
//    public String returnUrlMethod(HttpServletRequest request, HttpSession session, Model model) throws AlipayApiException, UnsupportedEncodingException {
//        System.out.println("=================================同步回调=====================================");
//
//        // 获取支付宝GET过来反馈信息
//        Map<String, String> params = new HashMap<String, String>();
//        Map<String, String[]> requestParams = request.getParameterMap();
//        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
//            String name = (String) iter.next();
//            String[] values = (String[]) requestParams.get(name);
//            String valueStr = "";
//            for (int i = 0; i < values.length; i++) {
//                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
//            }
//            // 乱码解决，这段代码在出现乱码时使用
//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
//            params.put(name, valueStr);
//        }
//
//        System.out.println(params);//查看参数都有哪些
//        //验证签名（支付宝公钥）
//        boolean signVerified = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET, SIGN_TYPE); // 调用SDK验证签名
//        //验证签名通过
//        if(signVerified){
//            // 商户订单号
//            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
//
//            // 支付宝交易流水号
//            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
//
//            // 付款金额
//            float money = Float.parseFloat(new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8"));
//
//            System.out.println("商户订单号="+out_trade_no);
//            System.out.println("支付宝交易号="+trade_no);
//            System.out.println("付款金额="+money);
//
//            //在这里编写自己的业务代码（对数据库的操作）
//			/*
//			################################
//			*/
//            //跳转到提示页面（成功或者失败的提示页面）
//            model.addAttribute("flag",1);
//            model.addAttribute("msg","支持");
//            return "common/payTips";
//        }else{
//            //跳转到提示页面（成功或者失败的提示页面）
//            model.addAttribute("flag",0);
//            model.addAttribute("msg","支持");
//            return "common/payTips";
//        }
//    }
//    @RequestMapping("/paytest")
//    public   void   alipay (HttpServletRequest httpRequest,
//                            HttpServletResponse httpResponse)   throws IOException {
//
//
//
//        //获得初始化的AlipayClient
//        AlipayClient alipayClient =  new DefaultAlipayClient( Constant.URL , Constant.APP_ID, Constant.APP_PRIVATE_KEY, Constant.FORMAT,
//                Constant.CHARSET, Constant.ALIPAY_PUBLIC_KEY, Constant.SIGN_TYPE);
//        //创建API对应的request
//        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest();
//        alipayRequest.setReturnUrl(Constant.RETURNURL);
//        //在公共参数中设置回跳和通知地址
//        alipayRequest.setNotifyUrl(Constant.NOTIFYURL);
//        String outTradeNo = RandomUtil.randomNumbers(15);
//        String totalAmount = "88.8";
//        alipayRequest.setBizContent( "{"  +
//                "    \"out_trade_no\":\""+outTradeNo+"\","  +
//                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
//                "    \"total_amount\":"+totalAmount+","  +
//                "    \"subject\":\"Iphone6 16G\","  +
//                "    \"body\":\"Iphone6 16G\","  +
////                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
//                "    \"extend_params\":{"  +
//                "    \"sys_service_provider_id\":\"2088511833207846\""  +
//                "    }" +
//                "  }" );
//        //填充业务参数
//        String form= "" ;
//        try  {
//            //调用SDK生成表单
//            form = alipayClient.pageExecute(alipayRequest).getBody();
//        }  catch  (AlipayApiException e) {
//            e.printStackTrace();
//        }
//        httpResponse.setContentType( "text/html;charset="  +  Constant.CHARSET);
//        //直接将完整的表单html输出到页面
//        httpResponse.getWriter().write(form);
//        httpResponse.getWriter().flush();
//        httpResponse.getWriter().close();
//    }
//
//
//}
